package Offer;

import java.util.HashMap;
import java.util.Map;

/**
 * 复杂链表的复制
 *
 * @author 是阿秋啊
 * @date 2022/03/29 20:28
 **/
public class Num35 {
    public Node copyRandomList(Node head) {
        Map<Node,Node> map = new HashMap<>();
        Node prev = head;
        while (prev != null) {
            Node node = new Node(prev.val);
            map.put(prev,node);
            prev = prev.next;
        }
        for (Node x = head; x != null ; x = x.next) {
            map.get(x).next = map.get(x.next);
            map.get(x).random = map.get(x.random);
        }
        return map.get(head);
    }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
